% Copyright (C) 2004 artofcode LLC. All rights reserved.
% 
% This software is provided AS-IS with no warranty, either express or
% implied.
% 
% This software is distributed under license and may not be copied,
% modified or distributed except as expressly authorized under the terms
% of the license contained in the file LICENSE in this distribution.
% 
% For more information about licensing, please refer to
% http://www.ghostscript.com/licensing/. For information on
% commercial licensing, go to http://www.artifex.com/licensing/ or
% contact Artifex Software, Inc., 101 Lucas Valley Road #110,
% San Rafael, CA  94903, U.S.A., +1(415)492-9861.

% $Id: mkcidfm.ps,v 1.1 2004/11/18 06:48:41 ghostgum Exp $

% Generate a cidfmap file for substituting CID fonts with TrueType 
% fonts, based on fonts found in the directory FONTDIR.
% 
% The directory FONTDIR is searched for fonts whose filename
% matches a Path in the cidsubs dictionary.
% Any matches are written out as a CID font substitution.
%
% For each fontname and alias in the fontaliases dictionary,
% write out each alias that matches a substituted font.
% Where multiple aliases are possible, use the first match.
%
% Note that the substitutions and aliases in this file were created 
% by someone who doesn't understand Chinese, Japanese or Korean.  
% The initial list contains only font files found in Windows XP.
% Please submit corrections and additions.
%
% Font filenames must match case.  
% All font filenames are currently lower case.
%
% Usage: gswin32c -q -dBATCH -sFONTDIR=c:/windows/fonts
%                -sCIDFMAP=c:/gs/cidfmap mkcidfm.ps


systemdict /FONTDIR known not { /FONTDIR (c:/windows/fonts) def } if

systemdict /CIDFMAP known { CIDFMAP } { (%stdout) } ifelse
/cidfmap exch (w) file def

% Directory separator as used by filenameforall
/dirsep (/) def

% This dictionary contains a list of font substitutions.
% The first matching substitution in the array will be used.
/fontaliases
<<
  % Traditional Chinese
  /MSung-Light [ /MingLiU ]
  /MSung-Medium [ /MingLiU ]
  /MHei-Medium [ /MingLiU ]
  /MKai-Medium [ /Arial-Unicode-MS /MingLiU ]

  % Simplified Chinese
  /STSong-Light [ /SimSun ]
  /STFangsong-Light [ /SimSun ]
  /STHeiti-Regular [ /SimHei ]
  /STKaiti-Regular [ /SimHei ]

  % Japanese
  /Ryumin-Light [ /MS-Mincho ]
  /Ryumin-Medium [ /MS-Mincho ]
  /GothicBBB-Medium [ /MS-Gothic ]
  /HeiseiMin-W3 [ /MS-Mincho ]
  /HeiseiKakuGo-W5 [ /MS-Gothic ]

  % Korean
  /HYSMyeongJo-Medium [ /Batang ]
  /HYRGoThic-Medium [ /Gulim ]
  /HYGoThic-Medium [ /Dotum ]
>>
def


% This dictionary contains a list of CID substitutions
/cidsubs 
<<
  % Simplified Chinese
  /SimHei
  <<
   /FileType /TrueType
   /Path (simhei.ttf)
   /SubfontID 0
   /CSI [(GB1) 2]
  >>

  /SimSun
  <<
   /FileType /TrueType
   /Path (simsun.ttc)
   /SubfontID 0
   /CSI [(GB1) 2]
  >>

  /NSimSun
  <<
   /FileType /TrueType
   /Path (simsun.ttc)
   /SubfontID 1
   /CSI [(GB1) 2]
  >>

  % Traditional Chinese
  /MingLiU
  <<
   /FileType /TrueType
   /Path (mingliu.ttc)
   /SubfontID 0
   /CSI [(CNS1) 2]
  >>

  /PMingLiU
  <<
   /FileType /TrueType
   /Path (mingliu.ttc)
   /SubfontID 1
   /CSI [(CNS1) 2]
  >>

  /Arial-Unicode-MS
  <<
   /FileType /TrueType
   /Path (arialuni.ttc)
   /SubfontID 0
   /CSI [(CNS1) 2]
  >>

  % Japanese
  /MS-Gothic
  <<
   /FileType /TrueType
   /Path (msgothic.ttc)
   /SubfontID 0
   /CSI [(Japan1) 3]
  >>

  /MS-PGothic
  <<
   /FileType /TrueType
   /Path (msgothic.ttc)
   /SubfontID 1
   /CSI [(Japan1) 3]
  >>

  /MS-UI-Gothic
  <<
   /FileType /TrueType
   /Path (msgothic.ttc)
   /SubfontID 2
   /CSI [(Japan1) 3]
  >>

  /MS-Mincho
  <<
   /FileType /TrueType
   /Path (msmincho.ttc)
   /SubfontID 0
   /CSI [(Japan1) 3]
  >>

  /MS-PMincho
  <<
   /FileType /TrueType
   /Path (msmincho.ttc)
   /SubfontID 1
   /CSI [(Japan1) 3]
  >>

  % Korean
  /Batang
  <<
   /FileType /TrueType
   /Path (batang.ttc)
   /SubfontID 0
   /CSI [(Korea1) 3]
  >>

  /BatangChe
  <<
   /FileType /TrueType
   /Path (batang.ttc)
   /SubfontID 1
   /CSI [(Korea1) 3]
  >>

  /Gungsuh
  <<
   /FileType /TrueType
   /Path (batang.ttc)
   /SubfontID 2
   /CSI [(Korea1) 3]
  >>

  /GungsuhChe
  <<
   /FileType /TrueType
   /Path (batang.ttc)
   /SubfontID 3
   /CSI [(Korea1) 3]
  >>

  /Gulim
  <<
   /FileType /TrueType
   /Path (gulim.ttc)
   /SubfontID 0
   /CSI [(Korea1) 3]
  >>

  /GulimChe
  <<
   /FileType /TrueType
   /Path (gulim.ttc)
   /SubfontID 1
   /CSI [(Korea1) 3]
  >>

  /Dotum
  <<
   /FileType /TrueType
   /Path (gulim.ttc)
   /SubfontID 2
   /CSI [(Korea1) 3]
  >>

  /DotumChe
  <<
   /FileType /TrueType
   /Path (gulim.ttc)
   /SubfontID 3
   /CSI [(Korea1) 3]
  >>

>>
def


% A dictionary for storing the names and paths of found fonts
/foundfonts 50 dict def

% Get the basename of a path
% For example, (c:/windows/fonts/msmincho.ps) --> (msmincho.ps)
/basename { % path -- subpath
  { dirsep search
    {pop pop}
    {exit}
    ifelse
  } loop
} def


% Check if a font basename matches one of the possible cid substitutions.  
% If it does, add the font and full path to the foundfonts dictionary
/checkfont {
  dup basename
  cidsubs
  {
    /Path get
    2 index eq  % Match case only
    {
      foundfonts exch
      3 index dup length string copy put
    }
    {
      pop
    }
    ifelse
  }
  forall
  pop pop
} def

% Add all matching fonts to foundfonts dictionary
/findfonts { % path --
  dup length 2 add string dup 3 1 roll copy pop
  dup dup length 2 sub dirsep 0 get put
  dup dup length 1 sub (*) 0 get put
  /checkfont load 256 string filenameforall
} def


% Write out a cid substition, using the full font path
% name is the font name, e.g. /MS-Mincho
% path is the full path to the font file, e.g. (c:\windows\fonts\msmincho.ps)
% subs is the dictionary for CID font substition, from cidsubs
/emitsubs { % name path subs -- 
  3 -1 roll cidfmap exch write==only % name
  cidfmap ( << ) writestring
  % path subs
  {
   1 index /Path eq {pop 1 index} if % use full path, not basename
   exch cidfmap exch write==only cidfmap ( ) writestring 
   cidfmap exch write==only cidfmap ( ) writestring 
  } forall
  cidfmap (>> ;\n) writestring
  pop
} def

% Write out all known cid substitutions.
/writesubs { % ---
  cidfmap (% Substitutions\n) writestring
  foundfonts
  {
    1 index cidsubs exch known
    {
      1 index cidsubs exch get % name path subs
      emitsubs
    }
    {
      pop pop
    }
    ifelse
  }
  forall
} def

% Write out aliases for which the cid substituted font exists.
/writealiases { % --
  cidfmap (\n% Aliases\n) writestring
  fontaliases 
  {
    % name aliasarray
    {
      % name alias
      foundfonts 1 index known
      {
	1 index cidfmap exch write==only cidfmap ( ) writestring 
	cidfmap exch write==only cidfmap ( ;\n) writestring
	exit % after first match
      }
      {
	pop % didn't find this name
      }
      ifelse
    } forall
    % name
    pop
  }
  forall
} def


% Write out a new cidfmap file to stdout
/writecidfmap { % path --
  cidfmap (%!\n% cidfmap generated automatically by mkcidfm.ps from fonts found in\n) writestring
  cidfmap (%   ) writestring
  dup cidfmap exch writestring cidfmap (\n\n) writestring
  findfonts
  writesubs
  writealiases
} def


FONTDIR writecidfmap
